package demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jweb.core.bean.PageMode;
import org.jweb.core.bean.ReplyDataMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.jweb.core.util.StringUtil;
import org.jweb.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.keepme.ep.auth.transfer.vo.authen.AuthUserVO;
import cn.keepme.ep.auth.transfer.vo.authen.UserCategoryVO;
import cn.keepme.ep.common.entity.DictType;
import cn.keepme.ep.common.service.SysServiceI;
import cn.keepme.ep.member.entity.GeneralUserEntity;
import cn.keepme.ep.member.entity.UserCategoryEntity;
import cn.keepme.ep.member.transfer.form.member.UserCategorySearchForm;
import demo.entity.NotifyEntity;
import demo.service.NotifyServiceI;
import demo.trans.form.NotifySearchForm;
import demo.trans.vo.NotifyVO;

/**
 * @Title: Controller
 * @Description: 喊单信息
 * @author zhangdaihao
 * @date 2015-09-09 12:05:32
 * @version V1.0
 *
 */
@Controller
@RequestMapping("/notifyController")
public class NotifyController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(NotifyController.class);

	@Autowired
	private NotifyServiceI notifyService;
	@Autowired
	private SysServiceI sysService;

	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 喊单信息列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "/notify")
	public ModelAndView notify(HttpServletRequest request) {
		// 验证是否登录
		// 获取当前登录用户
		Object object = request.getSession().getAttribute("userInfo");
		GeneralUserEntity currentUser = null;
		if (null == object) {
			// 跳转到登录页面
			return new ModelAndView("login.html");
		} else {
			currentUser = (GeneralUserEntity) object;
		}

		return new ModelAndView("listMsg.html");
	}

	/**
	 * 喊单信息列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/notifyList")
	@ResponseBody
	public ReplyDataMode notifyList(NotifySearchForm searchForm,int pageSize,int pageNo,HttpServletRequest request) {
		ReplyDataMode j = new ReplyDataMode();
		Subject currentUser = SecurityUtils.getSubject();
		AuthUserVO userEntity = (AuthUserVO)currentUser.getSession().getAttribute("userInfo");
		List<UserCategoryVO> ulist = getNotifySetingInfo(userEntity.getId());
		StringBuffer notifyCategoryArrStr = new StringBuffer();
		for(UserCategoryVO c : ulist){
			if("1".equals(c.getStatus())){
				notifyCategoryArrStr.append(c.getCode()).append(",") ;
			}
		}
		if(notifyCategoryArrStr.length() > 0){
			notifyCategoryArrStr.deleteCharAt(notifyCategoryArrStr.length()-1);
			searchForm.setCategoryCode(notifyCategoryArrStr.toString());
		}
		searchForm.setStatus("1");
		searchForm.emptyValue2NullValue();
		CriteriaQuery cq = searchForm.getCriteriaQuery();
		
		// 分页问题，已修改，by wupan
//		PageMode pageMode =this.sysService.list4page(cq, pageSize, pageNo);
		PageMode pageMode =this.sysService.list4page(cq, pageNo, pageSize);
		
		
		List<NotifyEntity> notifyEntityList = pageMode.getResult();
		List<NotifyVO> notifyVOList = new ArrayList<NotifyVO>();
		for(NotifyEntity n : notifyEntityList){
			NotifyVO vo = new NotifyVO();
			vo.copyEntity(n);
			
			notifyVOList.add(vo);
		}
		pageMode.setResult(notifyVOList);
		j.setData(pageMode);
		j.setSuccess(true);
		return j;
	}
	
	
	
//	private List<UserCategoryVO> getNotifySetingInfo(String mphone){
//		List<UserCategoryVO> ulist = new ArrayList<UserCategoryVO>();
//		try {
//			String sql = "select  t1.category_code ,t2.status from tp_user_category t2   LEFT JOIN tp_analyze_category t1  on t2.category_id = t1.id where t2.status = '1' and t2.general_user_id = '" + mphone + "'";
//			List<Map<String,Object>> list  = sysService.findForJdbc(sql, null);
//			UserCategoryVO vo = new UserCategoryVO();
//			for(Map<String,Object> l:list){
//				vo = new UserCategoryVO();
//				vo.setStatus(l.get("status").toString());
//				vo.setCode(l.get("category_code").toString());
//				ulist.add(vo);
//			}
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return ulist;
//	}
	
	private List<UserCategoryVO> getNotifySetingInfo(String userId){
		//获交易类型取数据字典
		List<DictType> tradeTypeDictTypeList = this.sysService.findDict("custom");
		Map<String,DictType> tradeTypeDictTypeMap = new HashMap<String,DictType>();
		for(DictType d : tradeTypeDictTypeList){
			tradeTypeDictTypeMap.put(d.getTypecode(), d);
		}
		
		UserCategorySearchForm searchForm = new UserCategorySearchForm();
		searchForm.setUserId(userId);
		CriteriaQuery cq = searchForm.getCriteriaQuery();
		List<UserCategoryEntity> userCategoryList = this.sysService.list(cq);
		
		List<UserCategoryVO> ulist = new ArrayList<UserCategoryVO>();
		for(UserCategoryEntity c : userCategoryList){
			UserCategoryVO vo = new UserCategoryVO();
//			vo.setStatus(c.getStatus());
			vo.setStatus(c.getTradeRoomIdStatus());
			vo.setCode(c.getCategoryCode());
			String code = c.getCategoryCode();
			if(StringUtil.isNotEmpty(code)){
				DictType temp = tradeTypeDictTypeMap.get(code);
				if(temp != null){
					vo.setName(temp.getTypename());
				}
			}
			ulist.add(vo);
		}
		
		return ulist;
		
	}
	
//	@RequestMapping(value = "/notifyList")
//	@ResponseBody
//	public ReplyDataMode notifyList(HttpServletRequest request) {
//		ReplyDataMode replyDataMode = new ReplyDataMode();
//		// 查询数据信息
//		String pageNo = request.getParameter("pageNo") == null ? "1" : request
//				.getParameter("pageNo");
//		String pageSize = request.getParameter("pageSize") == null ? "10"
//				: request.getParameter("pageSize");
//		String searchType = request.getParameter("type");
//		String searchStart = request.getParameter("searchStart");
//		String searchEnd = request.getParameter("searchEnd");
//		Map<String, String> searchMap = new HashMap<String, String>();
//		searchMap.put("pageNo", pageNo);
//		searchMap.put("pageSize", pageSize);
//		if (null != searchType) {
//			try {
//				searchMap.put("searchType", new String(searchType.getBytes("ISO-8859-1"),"UTF-8"));
//			} catch (UnsupportedEncodingException e) {
//				// TODO Auto-generated catch block
//				logger.error("转换查询类型出错:" + e.getMessage());
//			}
//		}
//		if(null != searchStart){
//			searchMap.put("searchStart", searchStart);
//		}
//		if(null != searchEnd){
//			searchMap.put("searchEnd", searchEnd);
//		}
//		
//		PageMode<NotifyEntity> pageMode = null;
//		try {
//			// 执行查询
//			pageMode = notifyService.query4Page(Integer.parseInt(pageNo),
//					Integer.parseInt(pageSize), searchMap);
//			replyDataMode.setSuccess(true);
//		} catch (Exception e) {
//			logger.error("加载喊单信息出错:" + e.getMessage());
//			replyDataMode.setSuccess(false);
//		} finally {
//			// 返回数据信息
//			replyDataMode.setData(pageMode);
//		}
//		return replyDataMode;
//	}
}
